const express = require("express")
const bodyParser = require("body-parser")
const path = require("path")

const userRouter = require("./router/userRouter")
const postRouter = require("./router/postRoute")

const app = express()
const port = 3001


// app.use("/",bodyParser.json())
// x-www-form-urlencoded 表单方式解析  
app.use(bodyParser.urlencoded({extend:false}))
// 解析json
app.use(bodyParser.json())
// 解析txt
app.use(bodyParser.text({type:'text/html'}))

// app.use("/",(req,res,next)=>{
//     //
//     // console.log(req);
//     if(req.body.token){
//         next()
//     }else{
//         res.send("no token")
//     }
// },userRouter)

app.use("/post",postRouter)

// static
const options = {
    dotfiles:'ignore',
    etag:false,
    extensions:['html'],
    index:false,
    maxAge:'1d',
    redirect:false,
    setHeaders:function(res,path,stat){
        res.set('x-timestamp',Date.now())
    }
}
// 获取绝对路径
console.log(__dirname) //D:\lzm\study\nodejs\express
// 拼接相对路径加 绝对路径 
//  内置模块 path
console.log(path.join(__dirname,"../img"))

app.use("/public",express.static(path.join(__dirname,"./public")))


app.listen(port,()=>{
    console.log(`serve listening on port ${port}`)
})